//
//  MyAnyTransitionExtension.swift
//  SwiftUIAnimation
//
//  Created by 李煜 on 2024/8/21.
//

import SwiftUI

struct MyAnyTransitionExtension: View {
  @State private var showPurple = false
  var body: some View {
    VStack {
      RoundedRectangle(cornerRadius: 10)
        .frame(width: 300, height: 300)
        .foregroundStyle(.green)
        .overlay {
          Text("green")
            .foregroundStyle(.white).font(.system(.largeTitle, design: .rounded))
            .bold()
        }
      if showPurple {
        RoundedRectangle(cornerRadius: 10)
          .frame(width: 300, height: 300)
          .foregroundStyle(.purple)
          .overlay {
            Text("purple ")
              .foregroundStyle(.white).font(.system(.largeTitle, design: .rounded))
              .bold()
          }
          // 使用自己的可重用转场
          .transition(.offsetScaleOpacity)
          
      }
    }
    .onTapGesture {
      withAnimation(.easeInOut(duration: 2)) {
        showPurple.toggle()
      }
    }
  }
}

#Preview {
  MyAnyTransitionExtension()
}

//扩展AnyTransition实现我自己可重用的转场
extension AnyTransition {
  static var offsetScaleOpacity: AnyTransition {
    .offset(x: -600, y: 0).combined(with: .scale).combined(with: .opacity)
  }
}
